import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import calendar
from datetime import datetime
import numpy as np
pd.options.mode.chained_assignment=None

all_df=pd.read_csv('train.csv')
# from sklearn.model_selection import train_test_split
# train_df,test_df=train_test_split(df,test_size=0.7)
#
# test_df['traintest']='test'
# train_df['traintest']='train'

# all_df=pd.concat((train_df,test_df))
#2011-01-01 01:00:00
#摘出日期
all_df['date']=all_df['datetime'].apply(
    lambda x:x.split()[0]
)
print(all_df['date'])
#摘出月份
all_df['month']=all_df['date'].apply(
    lambda x:x.split('-')[1] )
#画图 每个月租赁量

# all_df['month'].value_counts().sort_index().plot(kind='line')
# plt.show()
#每月第几天
all_df['daynum']=all_df['date'].apply(
    lambda x:x.split('-')[2]
)
#每天第几个小时
all_df['hour']=all_df['datetime'].apply(
    lambda x:int(x.split()[1].split(':')[0])
)

# print(all_df.head())
#周几
all_df['weekday']=all_df['date'].apply(
    lambda x:calendar.day_name[datetime.strptime(x,'%Y-%m-%d').weekday()]
)

#画出小时和租车量关系图
# all_df.groupby('hour').sum()['count'].sort_index().plot(kind='line')
# plt.show()
# 建立映射函数
#将24小时 转化为时间段
def a(x):
    if x>=0 and x<=6:
        return 0
    elif x>=7 and x<=10:
        return 1
    elif x>=11 and x<=15:
        return 2
    elif x>=16 and x<=20:
        return 3
    else: return 4

#将24小时转为时间段
all_df['hour_section']=all_df['hour'].apply(a)

#箱线图
#只看租赁量
sns.boxplot(data=all_df,y='count')
plt.show()
#租赁量和季节
sns.boxplot(data=all_df,y='count',x='season')
plt.show()
#租赁量和小时
sns.boxplot(data=all_df,y='count',x='hour')
plt.show()
#租赁量和是否工作日
sns.boxplot(data=all_df,y='count',x='workingday')
plt.show()
#通过3sigma法则
#先算均值
miu=all_df['count'].mean()
#标准差
sigma=all_df['count'].std()
#好点 非噪声点
#样本点减去均值后 小余 3倍的标准差
goodpoint=all_df[np.abs(all_df['count']-miu)<(3*sigma)]
#非噪声点个数
goodpoint_len=len(goodpoint)
#噪声点
#样本点减去均值后 大余 3倍的标准差
badpoint=all_df[np.abs(all_df['count']-miu)>(3*sigma)]
#噪声点个数
badpoint_len=len(badpoint)
#输出非噪声点比例
print(goodpoint_len/(goodpoint_len+badpoint_len))
#将数据集去除噪声点
all_df=goodpoint